package com.quik.web.product.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.quik.common.Constants;
import com.quik.dto.PaginationDto;
import com.quik.entity.DtaProduct;
import com.quik.entity.SysAccount;
import com.quik.exception.ErrorCode;
import com.quik.web.product.service.ProductService;

/**
 * 功能说明：
 * 		   系统使用商品管理
 * @author jenny
 *
 */
@Controller
@RequestMapping("/product")
public class ProductController {
	
	private static Logger log = (Logger)Logger.getLogger(ProductController.class);
	
	@Autowired
	private ProductService productService;
	
	/**
	 * 进入产品管理界面
	 * @return
	 */
	@RequestMapping(value = "productList", method = RequestMethod.GET)
	public ModelAndView productList(){
		ModelAndView mav = new ModelAndView("/product/productList");
		
		try{
			List categoryList = productService.findCategory();
			
			mav.addObject("categoryList", categoryList);
		}catch(Exception e){
			e.printStackTrace();
			log.error("ProductController productList error: " + e.getMessage());
		}
		
		return mav;
	}
	
	/**
	 * 根据商品类型、商品名称及编号查询商品列表
	 * @param page
	 * @param rows
	 * @param categoryId
	 * @param subCategoryId
	 * @param productNo
	 * @param productName
	 * @return
	 */
	@RequestMapping(value = "queryProduct", method = RequestMethod.POST)
	@ResponseBody                                                         
	public PaginationDto queryProduct(Integer page, Integer rows, Integer categoryId, Integer subCategoryId,
			String productNo, String productName){
		PaginationDto dto = new PaginationDto(0, null);
		
		try{
			int intPage = (page == null || page == 0) ? 1:page;  //当前页
		    int pageSize = (rows == null || rows == 0) ? Constants.TREE_PAGE_SIZE : rows;  //每页显示的记录
			
		    int totalCount = productService.findProductCount(categoryId, subCategoryId, productNo, productName);
		    List list = productService.findProduct(categoryId, subCategoryId, productNo, productName, intPage, pageSize);
		    
		    dto.setTotal(totalCount);
		    dto.setRows(list);
		}catch(Exception e){
			e.printStackTrace();
			log.error("ProductController queryProduct error: " + e.getMessage());
		}
		
		return dto;
	}
	
	/**
	 * 进入新增商品界面
	 * 	   //商品大类及商品子类的二级联动
	 * @return
	 */
	@RequestMapping(value = "initAddProduct")
	public ModelAndView initAddProduct(HttpServletRequest request){
		ModelAndView mav = new ModelAndView("/product/addProduct");
		String result = (String)request.getAttribute("result");
		
		try{
			List categoryList = productService.findCategory();
			
			if(result != null && result.length() > 0)
				mav.addObject("result", result);
			
			mav.addObject("categoryList", categoryList);
			mav.addObject("abled", DtaProduct.OPTION_FOR_YES);
			mav.addObject("unabled", DtaProduct.OPTION_FOR_NO);
		}catch(Exception e){
			e.printStackTrace();
			log.error("ProductController initAddProduct error: " + e.getMessage());
		}
		
		return mav;
	}
	
	/**
	 * 根据类别ID获取子类
	 * @return
	 */
	@RequestMapping(value = "getSubCategory", method = RequestMethod.POST)
	@ResponseBody
	public List getSubCategory(Integer categoryId){
		List list = null;
		
		try{
			list = productService.findSubCategoryByCategoryId(categoryId);
		}catch(Exception e){
			e.printStackTrace();
			log.error("ProductController getSubCategory error: " + e.getMessage());
		}
		
		return list;
	}
	
	/**
	 * 检查商品编号是否重复
	 * @param productNo
	 * @return
	 */
	@RequestMapping(value = "checkProduct", method = RequestMethod.POST)
	public ResponseEntity<String> checkProductNo(String productNo){
		boolean result = false;
		
		try{
			result = productService.checkProductNo(productNo);
		}catch(Exception e){
			e.printStackTrace();
			log.error("ProductController checkProductNo error: " + e.getMessage());
		}
		
		return new ResponseEntity<String>(String.valueOf(result), HttpStatus.OK);
	}
	
	/**
	 * 新增商品
	 * @param product
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "addProduct", method = RequestMethod.POST)
	public ModelAndView addProduct(DtaProduct product, HttpServletRequest request){
		ModelAndView mav = new ModelAndView("/product/addProduct");
		String result = ErrorCode.RESULT_ADD_PRODUCT_FAILED;
		SysAccount account = (SysAccount)request.getSession().getAttribute(Constants.SESSION_ACCOUNT);
		
		try{
			product.setCrt_user(account.getStaff_no());
			product.setCrt_date(new java.util.Date(System.currentTimeMillis()));
			
			productService.addProduct(product);
			
			result = ErrorCode.RESULT_ADD_PRODUCT_SUCCESS;
			
			mav.addObject("result", result);
			mav.setViewName("forward:/product/initAddProduct");
		}catch(Exception e){
			e.printStackTrace();
			log.error("ProductController addProduct error: " + e.getMessage());
		}
		
		return mav;
	}
	
	
}
